/*
* 1、
*（1）删除字符串两端的空格  
*（2）编写测试用例验证代码正确性
*/

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

void Space(char* soucre);

bool GTest(void);

int main() {
    bool result = GTest();
    if (result) {
        printf("All the test cases passed!");
    }else {
        printf("The test case failed, please debug and modify the code!");
    }

    return 0;
}

bool GTest(void) {
    char Testcase[4][50] = { {"Hellow,world 123!    "}, {"Hellow,world 123!"}, {"     Hellow,world 123!"}, {"     Hellow,world 123!     "} };
    char Caseresult[4][50] = { {"Hellow,world 123!"}, {"Hellow,world 123!"}, {"Hellow,world 123!"}, {"Hellow,world 123!"} };

    for (int i = 0; i < 4; i++) {
        Space(Testcase[i]);
        if (strcmp(Testcase[i], Caseresult[i])) {
            return false;
        }
    }
    return true;
}

void Space(char* soucre) {

    if (soucre == NULL) {
        printf("Error in passing parameters \n");
        return;
    }
    char* pev = soucre;
    char* end = soucre + (strlen(soucre) - 1);
    char Buf[100] = { 0 };

    while (*pev == ' ' && pev <= end) {
        pev++;
    };

    while (*end == ' ' && end >= pev) {
        end--;
    };

    int len = 0;

    if (pev <= end) {
        len = end - pev + 1;
        strncpy_s(Buf, 100,pev, len);
        sprintf(soucre, Buf);
    }
    else {
        printf("No valid characters\n");
    }
}